package com.tadpole.music.presenter.login;

import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.login.RegisterIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 注册
 * Created By 孙岩
 * on 2020/9/15
 */
public class RegisterPresenter extends BasePresenter<RegisterIView> {
    @SuppressWarnings("unchecked")
    public void register(String phone, String password, String code, String role) {
        if (isViewAttached()) {
            String url = UrlConfig.register;
            DataModel.request(ModelToken.POST_REQUEST)
                    .url(url)
                    .paramKey("phone", "password", "code", "role")
                    .paramValue(phone, password, code, role)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    getView().showResult();
                                } else if(obj.getInt("code")==401){
                                    getView().show401();
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
